﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Hotel.DichVu
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private SqlConnection con;
        private SqlDataAdapter da = new SqlDataAdapter();
        QL_dichvu quanly = new QL_dichvu();

        private Object ListDichVu()
        {
            String sql = "select * from KhoDichVu where DaXoa = 0";
            String sqlConnectString = "Data Source=localhost;Initial Catalog=Hotel;Integrated Security=True";
            SqlConnection con = new SqlConnection(sqlConnectString);
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        private Object ListNhanVien()
        {
            String sql = "select * from NhanVien where DaXoa = 0";
            String sqlConnectString = "Data Source=localhost;Initial Catalog=Hotel;Integrated Security=True";
            SqlConnection con = new SqlConnection(sqlConnectString);
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        private Object ListPhong()
        {
            String sql = "select * from Phong where DaXoa = 0";
            String sqlConnectString = "Data Source=localhost;Initial Catalog=Hotel;Integrated Security=True";
            SqlConnection con = new SqlConnection(sqlConnectString);
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        private void connect()
        {
            string cn = "Data Source=.;Initial Catalog=Hotel;Integrated Security=True";

            try
            {
                con = new SqlConnection(cn);
                con.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Khong the ket noi co so du lieu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void serverdata(string sql)
        {
            try
            {
                connect();
                SqlCommand command = new SqlCommand();
                command.Connection = con;
                command.CommandText = sql;
                command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                MessageBox.Show("Sai thao tác với Cơ sở dữ liệu!!!", "Lỗi Cơ sở dữ liệu", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnXuatKhoReset_Click(object sender, EventArgs e)
        {
            //textBox1.ResetText();
            tbxXuatKhoNhapSoLuong.ResetText();
            //textBox3.ResetText();
            //textBox4.ResetText();
            //textBox5.ResetText();
        }

        private void btnXuatKhoThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void tbxXuatKhoNhapSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !((e.KeyChar >= 48) && (e.KeyChar <= 57) || (e.KeyChar == 8));
        }

        private void Form2_Load(object sender, EventArgs e)
        {

            cbxXuatKhoChonTenHang.DataSource = ListDichVu();
            cbxXuatKhoChonTenHang.DisplayMember = "TenHang";
            cbxXuatKhoChonTenHang.SelectedIndex = -1;
            cbxXuatKhoChonTenNV.DataSource = ListNhanVien();
            cbxXuatKhoChonTenNV.DisplayMember = "TenNV";
            cbxXuatKhoChonTenNV.SelectedIndex = -1;
            cbxXuatKhoChonMaPhong.DataSource = ListPhong();
            cbxXuatKhoChonMaPhong.DisplayMember = "MaPhong";
            cbxXuatKhoChonMaPhong.SelectedIndex = -1;

        }

        private void btnXuatKho_Click(object sender, EventArgs e)
        {
            if ((tbxXuatKhoNhapSoLuong.Text != "") && (cbxXuatKhoChonTenHang.Text != ""))
                //&& (cbxXuatKhoChonTenNV.Text != "") && (dtpXuatKhoChonNgay.Text != "") && (cbxXuatKhoChonMaPhong.Text != ""))
            {
                int a = Convert.ToInt32(tbxXuatKhoNhapSoLuong.Text);
                int b = Convert.ToInt32(LaySoLuongDichVu(cbxXuatKhoChonTenHang.Text));
                double c = Convert.ToDouble(LayDonGiaDichVu(cbxXuatKhoChonTenHang.Text));
                double d = b * c;
                DateTime ngay = Convert.ToDateTime(dtpXuatKhoChonNgay.Text);
                if (b >= a)
                {
                    serverdata("UPDATE KhoDichVu SET SoLuong=SoLuong-'" + a + "' WHERE TenHang='" + cbxXuatKhoChonTenHang.Text + "'");
                    //MessageBox.Show(cbxXuatKhoChonTenNV.Text);
                    /*serverdata("INSERT INTO [Hotel].[dbo].[DichVuPhong]"
                                    + " ([MaHang]"
                                    + " ,[SoLuong]"
                                    + " ,[MaPhong]"
                                    + " ,[TongTien]"
                                    + " ,[MaNV]"
                                    + " ,[NgaySuDung]"
                                    + " ,[DaXoa])"
                                + " VALUES"
                                    + " ('" + LayMaHangDichVu(cbxXuatKhoChonTenHang.Text) + "'"
                                    + " ," + b + ""
                                    + " ,'" + cbxXuatKhoChonMaPhong.Text + "'"
                                    + " ," + d + ""
                                    + " ,'" + LayMaNhanVien(cbxXuatKhoChonTenNV.Text) + "'"
                                    + " ,'" + ngay + "'"
                                    + " ,0)");*/
                    MessageBox.Show("Xuất kho thành công", "Thông báo!!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Dispose();
                }
                else MessageBox.Show("Lỗi xuất kho!!!Số lượng dịch vụ không đủ để xuất", "Thông báo lỗi!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else MessageBox.Show("Mời nhập đầy đủ thông tin cho phiếu nhập kho", "Lỗi thiếu thông tin", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private String LaySoLuongDichVu(String tenhang)
        {
            String sql = "Select SoLuong from KhoDichVu where TenHang = '" + tenhang + "'";
            String sqlConnectString = "Data Source=localhost;Initial Catalog=Hotel;Integrated Security=True";
            SqlConnection con = new SqlConnection(sqlConnectString);
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            DataTable dt = new DataTable();
            dt.Load(cmd.ExecuteReader());
            return dt.Rows[0][0].ToString();
        }

        private String LayDonGiaDichVu(String tenhang)
        {
            String sql = "Select DonGia from KhoDichVu where TenHang = '" + tenhang + "'";
            String sqlConnectString = "Data Source=localhost;Initial Catalog=Hotel;Integrated Security=True";
            SqlConnection con = new SqlConnection(sqlConnectString);
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            DataTable dt = new DataTable();
            dt.Load(cmd.ExecuteReader());
            return dt.Rows[0][0].ToString();
        }

        private String LayMaHangDichVu(String tenhang)
        {
            String sql = "Select MaHang from KhoDichVu where TenHang = '" + tenhang + "'";
            String sqlConnectString = "Data Source=localhost;Initial Catalog=Hotel;Integrated Security=True";
            SqlConnection con = new SqlConnection(sqlConnectString);
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt.Rows[0][0].ToString();
        }

        private String LayMaNhanVien(String tennhanvien)
        {
            String sql = "Select MaNV from NhanVien where TenNV = '" + tennhanvien + "'";
            String sqlConnectString = "Data Source=localhost;Initial Catalog=Hotel;Integrated Security=True";
            SqlConnection con = new SqlConnection(sqlConnectString);
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt.Rows[0][0].ToString();
        }
    }
}
